const http = require("http")
const fs = require("fs")
const mysql = require("mysql");
const ejs = require("ejs")

// 创建数据连接
const conn = mysql.createConnection({
    host: "127.0.0.1",
    user: "root",
    password: "root",
    database: "stusys"
})

// 连接数据库
conn.connect((err) => {
    if (err) throw err
});



const server = http.createServer((req, res) => {
    res.setHeader("content-type", "text/html;charset=utf8")

    req.path = req.url.split("?")[0];
    switch (req.path) {
        case "/":
            //res.end("登录页面")
            res.end(fs.readFileSync("./views/index.html"));
            break;
        case "/login":
            let postData = "";

            req.on("data", (chunk) => {
                postData += chunk.toString();
            })

            req.on("end", () => {
                console.log(postData);
                let myurl = "http://localhost:8080/?" + postData;
                let myURL = new URL(myurl);

                let username = myURL.searchParams.get("username");
                let password = myURL.searchParams.get("password");

                // 读取数据库
                let sql = "select * from  admin where adminname=? and adminpwd=?";
                conn.query(sql, [username, password], (err, data) => {
                    if (err) throw err;
                    if (data.length > 0) {
                        let user = {
                            username: username,
                            status: 0, // 登录成功
                        }
                        res.end(JSON.stringify(user));
                    } else {
                        let user = {
                            username: username,
                            status: 1,  // 登录失败
                            msg: "用户名或密码错误"
                        }
                        res.end(JSON.stringify(user))
                    }
                })
            })
            break;
        case "/main":
            //res.end("主页面页面")
            res.end(fs.readFileSync("./views/main.html"));
            break;
        case "/list":
            // 数据库取数据
            let sql = "select * from stu";
            conn.query(sql, (err, data) => {
                console.log(data)
                ejs.renderFile("./views/list.html", { stus: data }, (err, data) => {
                    if (err) throw err;
                    res.end(data);
                })
            })
            break;
        case "/add":
            let url="http://localhost:8080/"+req.url;
            let myURL=new URL(url);
            let stuname=myURL.searchParams.get("stuname");
            let stuage=myURL.searchParams.get("stuage");
            let stusex=myURL.searchParams.get("stusex");
            let mobile=myURL.searchParams.get("mobile");
            let address=myURL.searchParams.get("address");
            
            let insertSql="insert into stu(stuname,stuage,stusex,mobile,address) values(?,?,?,?,?)"

            conn.query(insertSql,[stuname,stuage,stusex,mobile,address],(err,data)=>{
                if(err) throw err;
                res.end("<h1>添加成功!<a href='/list'>返回列表</a></h1>");
            })
            break;
        case "/toAdd":
            res.end(fs.readFileSync("./views/add.html"));
            break;
        case "/upd":
            res.end("修改学生")
            break;
        case "/delById":
            // 获取id
            let url02="http://localhost:8080/"+req.url;
            let myURL02=new URL(url02);
            let id=myURL02.searchParams.get("id");
            console.log(id);
            // 从数据库中删除
            let mysql="delete from stu where stuid=?";
            conn.query(mysql,[id],(err,data)=>{
                if(err) throw err;
                res.end("<h1>删除成功!<a href='/list'>返回列表</a></h1>");
            })
            break;
        case "/findById":
             // 获取id
             let url03="http://localhost:8080/"+req.url;
             let myURL03=new URL(url03);
             let id3=myURL03.searchParams.get("id");
             console.log(id3);
             // 根据id 查询学生对象
            let fsql="select * from stu where stuid=?"
            conn.query(fsql,id3,(err,data)=>{
                ejs.renderFile("./views/show.html", { stu: data[0] }, (err, data) => {
                    if (err) throw err;
                    res.end(data);
                })
            })
            break;
        default:
            res.end("页面不存在")
            break
    }
})



server.listen(8080, () => {
    console.log("server start up......")
})